package algorithm.MonotonePriorityStack.LeftRightLess;

import java.io.*;
import java.util.Stack;

/**
 * description :
 * author Ahang :
 * create : 2025-08-21 11:48
 */
public class Main {
    public static int[] arr;
    public static int n;
    public static int[][] ans;

    public static void main(String[] args) throws IOException {
        // 使用BufferedReader替代Scanner提高输入效率
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        arr = new int[n];

        String[] input = br.readLine().split(" ");
        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(input[i]);
        }
        br.close();

        compute();
    }

    static public void compute() {
        Stack<Integer> stack = new Stack<>();
        ans = new int[n][2];

        // 初始化数组
        for (int i = 0; i < n; i++) {
            ans[i][0] = -1;
            ans[i][1] = -1;
        }

        for (int i = 0; i < n; i++) {
            while (!stack.isEmpty() && arr[stack.peek()] >= arr[i]) {
                int temp = stack.pop();
                ans[temp][0] = stack.isEmpty() ? -1 : stack.peek();
                ans[temp][1] = i;
            }
            stack.push(i);
        }

        while (!stack.isEmpty()) {
            int temp = stack.pop();
            ans[temp][0] = stack.isEmpty() ? -1 : stack.peek();
            ans[temp][1] = -1;
        }

        for (int i = n - 2; i >= 0; i--) {
            if (ans[i][1] != -1 && arr[ans[i][1]] == arr[i]) {
                ans[i][1] = ans[ans[i][1]][1];
            }
        }

        // 使用StringBuilder提高输出效率
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(ans[i][0]).append(" ").append(ans[i][1]).append("\n");
        }
        System.out.print(sb.toString());
    }
}
